import json

from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.item.models import Subject


class TestView(View):
    def get(self, request):
        sub = Subject.objects.order_by('?')[:10]
        sub_list = []
        for num, i in enumerate(sub):
            print(type(i.answer), "*" * 10, i.type)
            if i.type == '0':
                sub_list.append({
                    'id': i.id,
                    'num': num + 1,
                    'types': '单选题',
                    'title': i.title,
                    'difficulty': i.difficulty.name,
                    'analysis': i.analysis,
                    'answer': i.answer,
                    'choice_a': i.choice_a,
                    'choice_b': i.choice_b,
                    'choice_c': i.choice_c,
                    'choice_d': i.choice_d,
                    'mark': 5
                })

                print(i.id)
            elif i.type == '1':
                sub_list.append({
                    'id': i.id,
                    'num': num + 1,
                    'types': '多选题',  # 类型
                    'title': i.title,  #
                    'difficulty': i.difficulty.name,
                    'analysis': i.analysis,
                    'answer': i.answer,
                    'choice_a': i.choice_a,
                    'choice_b': i.choice_b,
                    'choice_c': i.choice_c,
                    'choice_d': i.choice_d,
                    'choice_f': i.choice_c,
                    'choice_e': i.choice_e,
                    'mark': 5
                })
            elif i.type == '4':
                sub_list.append({
                    'id': i.id,
                    'num': num + 1,
                    'types': '判断题',
                    'title': i.title,
                    'difficulty': i.difficulty.name,
                    'analysis': i.analysis,
                    'answer': i.answer,
                    'boolt': i.boolt,
                    'boolf': i.boolf,
                    'mark': 5
                })
            elif i.type == '2':
                sub_list.append({
                    'id': i.id,
                    'num': num + 1,
                    'types': '问答题',
                    'title': i.title,
                    'difficulty': i.difficulty.name,
                    'analysis': i.analysis,
                    'note': i.note,
                    'mark': 5
                })

        print(len(sub_list), "-" * 10)
        context = {
            'sub_list': sub_list,  # 分页后数据
        }
        return http.HttpResponse(json.dumps(context), content_type="application/json")
